package cn.wolfcode.luowowo.website_api.controller;

import cn.wolfcode.luowowo.QueryObject.BannerQuery;
import cn.wolfcode.luowowo.QueryObject.TravelQuery;
import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.domain.*;
import cn.wolfcode.luowowo.redis.redisService.IStrategyStatisVoService;
import cn.wolfcode.luowowo.service.IBannerService;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.util.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyStatisVoService strategyStatisVoService;


    @GetMapping("/query")
    public Object query() {

        //游记分页查询 返回page给前端首页的列表
        TravelQuery qo = new TravelQuery();
        Page<Travel> page = travelService.query(qo);

        List<Banner> travelBanners = bannerService.queryBanner(Banner.TYPE_TRAVEL, Banner.STATE_NORMAL);//游记
        Banner strategyBanner = bannerService.queryBanner(Banner.TYPE_STRATEGY, Banner.STATE_NORMAL).get(0);//攻略
        return JsonResult.success(new ParamMap()
                .put("page", page)
                .put("stBanner", strategyBanner)
                .put("banners", travelBanners)
        );
    }

    //跳转游记编辑
    @GetMapping("/input")
    public Object input(String id) {
        ParamMap paramMap = new ParamMap();
        //更新时 过去当前的游记对象
        if (StringUtils.hasLength(id)) {
            Banner banner = bannerService.get(id);
            paramMap.put("tv", banner);
        }
        //获取全部目的地
        List<Destination> destinations = destinationService.list();
        paramMap.put("dests", destinations);
        return JsonResult.success(paramMap);
    }

    //游记新增
    @PostMapping("/saveOrUpdate")
    @RequireLogin
    public Object saveOrUpdate(Banner banner, @UserParam UserInfo userInfo) {
        bannerService.saveOrUpdate(banner);
        return JsonResult.success();
    }


    //上传图片
    @PostMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }
}
